<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectName',{rules: [{ required: true, message: '请输入项目名称', trigger: 'blur' }]}]" placeholder="请输入项目名称"></a-input>
        </a-form-item>
        <a-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectType',{rules: [{ required: true, message: '请输入项目类型', trigger: 'blur' }]}]" placeholder="请输入项目类型"></a-input>
        </a-form-item>
        <a-form-item label="项目来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectFrom',{rules: [{ required: true, message: '请输入项目来源', trigger: 'blur' }]}]" placeholder="请输入项目来源"></a-input>
        </a-form-item>
        <a-form-item label="项目金额（万元）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectAmount',{rules: [{required: true, message: '项目金额', trigger: 'blur'},
        {validator:this.isNumber}]}]" placeholder="请输入项目金额" type='number' ></a-input>
        </a-form-item>
        <a-form-item label="项目简要说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['projectBrief',{rules: [{ required: true, message: '请输入项目简要说明', trigger: 'blur' }]}]" rows="4" placeholder="请输入项目简要说明"/>
        </a-form-item>
        <a-form-item label="专家到场地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['professorWorkAddress',{rules: [{ required: true, message: '请输入专家到场地点', trigger: 'blur' }]}]" placeholder="请输入专家到场地点"></a-input>
        </a-form-item>
          <a-form-item required="true"  label="专家类型和对应人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <DynamicForm
              :data="childArray"
              v-decorator="['projectOwnProfessorType']" ref="DynamicForm">
            </DynamicForm>
          </a-form-item>
        <a-form-item required="true" label="专家到场时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date
            :showTime="true"
            dateFormat="YYYY-MM-DD HH:mm:ss"
            @change="disablePreDate"
            v-decorator="['professorWorkBeginTime']">
          </j-date>
        </a-form-item>
        <a-form-item label="项目预计评审时长(小时)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectExpectTime',{rules: [{required: true, message: '项目预计评审时长', trigger: 'blur'},
        {validator:this.isNumber}]}]" placeholder="请输入预计评审时长" type='number'></a-input>
        </a-form-item>
        <a-form-item label="项目附件压缩包" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['projectFile']" :trigger-change="true" :number="1"></j-upload>
          <label style='color: red'>（不超过10MB）</label>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import DynamicForm from '../modules/DynamicForm'
  import {httpAction, postAction,getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import JDate from '@/components/jeecg/JDate'
  import moment from 'moment'
  export default {
    name: "ProjectProfessorInfoModal",
    components: {
      JUpload,
      JCategorySelect,
      JDate,
      DynamicForm,
    },
    data () {
      return {
        projectinfo:{},
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        isNull:true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/projectProfessorInfo/add",
          edit: "/projectProfessorInfo/edit",
          selectByName: "/projectProfessorInfo/selectByName",
          selectBypro:"/projectProfessorInfo/selectbyprojectid",
        },
        childArray:[],
        childArrays:[],
      }
    },
    created () {
    },
    methods: {
      notNull(rule,value,callback){
        console.log(value)
        callback("ss");
      },
      isNumber(rule,value,callback){
        let numberReg = /^\d+$|^\d+[.]?\d+$/
        if (value !== '') {
          if (!numberReg.test(value)) {
            return callback('只能输入数字')
          } else {
            return callback()
          }
        } else {
          return callback()
        }
      },
      disablePreDate(current){
        if(current && current < (moment().endOf('day').format("YYYY-MM-DD HH:mm:ss"))){
          this.$message.error("专家到场时间最早为明天")
        }
      },
      add () {
        this.edit({});
        this.childArray=[];
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        console.log(this.model)
        let id=this.model.id;
        console.log("id")
        console.log(id)
        if(record!={}){
            getAction(this.url.selectBypro+"/"+id,"").then((res)=>{
              console.log(res)
              this.childArray=res.result;
            })
          // this.childArray=this.model.projectOwnProfessorType;
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'projectAmount','projectExpectTime','projectFrom','projectOwnProfessorType','projectName','projectType','professorType','professorNumber','projectBrief','projectFile','professorWorkBeginTime','professorWorkAddress'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        console.log("childArray")
        console.log(this.childArray)
        const that = this;
        // 触发表单验证
        this.childArray=this.$refs.DynamicForm.data;
        let childData = this.$refs.DynamicForm.data;
        this.form.setFieldsValue({
          projectOwnProfessorType: childData
        })
        this.form.validateFields((err, values) => {
          let formData = Object.assign(this.model, values);
          let falg=false;
          let falg2=true;
          let numberReg = /^\d+$|^\d+[.]?\d+$/
          console.log("看看这儿")
          console.log(formData.projectOwnProfessorType)
          for (let i = 0; i < formData.projectOwnProfessorType.length; i++) {
            let number1=parseInt(formData.projectOwnProfessorType[i].professorTypeName.substring(formData.projectOwnProfessorType[i].professorTypeName.length-2,formData.projectOwnProfessorType[i].professorTypeName.length-1))
            let number2=parseInt(formData.projectOwnProfessorType[i].professorNumber)
            if(formData.projectOwnProfessorType[i].professorType===''){
              falg=false;
              break;
            }else if(formData.projectOwnProfessorType[i].professorNumber==="0" || formData.projectOwnProfessorType[i].professorNumber==='' || !numberReg.test(formData.projectOwnProfessorType[i].professorNumber)){
              falg=false;
              break;
            }
            else
            {
              falg=true

              if(number1<number2){
                falg2=false
              }
            }
          }
          //cont.replace(/(^\s*)|(\s*$)/g) === '' || cont.toString()=== ''||cont.toString()===undefined ||cont===''
          if(formData.professorWorkAddress === undefined || formData.projectAmount === undefined
          || formData.projectBrief === undefined || formData.projectExpectTime === undefined
            || formData.projectName === undefined || formData.projectType === undefined ||
            formData.projectFrom === undefined
          ){
            this.$message.error("信息不可为空，请填写！");
          }
          else if(!falg){
             this.$message.error("请选择专家类型和对应人数");
          }
          else if(!falg2){
            this.$message.error("专家类型的对应人数不能大于专家库中所拥有的人数");
          }
           else if(formData.professorWorkBeginTime> (moment().endOf('day').format("YYYY-MM-DD HH:mm:ss"))){
            console.log("err是什么")
             console.log(err)
              if (!err) {
                that.confirmLoading = true;
                let httpurl = '';
                let method = '';
                if(!this.model.id){
                  httpurl+=this.url.add;
                  method = 'post';
                  getAction(this.url.selectByName+"/"+formData.projectName,"").then((res)=>{
                    if(res.success){
                      httpAction(httpurl,formData,method).then((res)=>{
                        if(res.success){
                          that.$message.success("操作成功!");
                          that.$emit('ok');
                          // this.$emit('Sampling',res.result);
                        }else{
                          console.log("11111111111")
                          console.log(res)
                          that.$message.warning("操作失败!");
                        }
                      }).finally(() => {that.confirmLoading = false;that.close();})
                    }
                    else{
                      that.$message.warning("不能建立具有相同项目名的项目");
                      that.confirmLoading = false;
                      that.close();
                    }
                  })
                }
                else{
                  httpurl+=this.url.edit;
                  method = 'put';
                  httpAction(httpurl,formData,method).then((res)=>{
                    if(res.success){
                      that.$message.success("操作成功!");
                      that.$emit('ok');
                      this.$emit('Sampling',res.result);
                    }else{
                      that.$message.warning(res.message);
                    }
                  }).finally(() => {that.confirmLoading = false;that.close();})
                }
              }
            }else {
              this.$message.error("专家到场时间最早为明天");
            }
        })
      },
      handleCancel () {
        this.childArray=this.model.projectOwnProfessorType;
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'projectAmount','projectExpectTime','projectFrom','projectOwnProfessorType','projectName','projectType','professorType','professorNumber','projectBrief','projectFile','professorWorkBeginTime','professorWorkAddress'))
      },
    }
  }
</script>
<style>
.ant-form-item-label{
  padding-top: 0px!important;
}
</style>